package control;

import entities.Folder;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import util.ServletUtil;

@WebServlet(name="EditFolder", urlPatterns={"/EditFolder"})
public class EditFolder extends HttpServlet {
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        boolean formSuccess = false;
        List<String> formErrors = new LinkedList<String>();
        request.setAttribute("formErrors", formErrors);
        request.setAttribute("headJsUrls", new String[]{"js/editFolder.js"});
        
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("vh11_WebApplication1PU");
        EntityManager em = fact.createEntityManager();
        
        try {
            // De te wijzigen Folder bean ophalen
            long folderId = ServletUtil.getLong(ServletUtil.getCookie(request, "currentFolderId"), -1);
            Folder folder = em.find(Folder.class, folderId);
            request.setAttribute("folder", folder);
            
            if (folder != null) {
                // Form gepost?
                if (request.getMethod().equals("POST")) {
                    // Ja: Data afhandelen

                    // Gekozen parent folder bean ophalen
                    long pFolderId = ServletUtil.getLong(request.getParameter("parentId"), -1);
                    Folder parentFolder = em.find(Folder.class, pFolderId);

                    try {
                        em.getTransaction().begin();
                        folder.setName(request.getParameter("name"));
                        folder.setParent(parentFolder);
                        em.getTransaction().commit();
                        
                        formSuccess = true;
                    }
                    catch(RollbackException ex) {
                        if (ex.getCause() instanceof ConstraintViolationException) {
                            ConstraintViolationException cve = (ConstraintViolationException)ex.getCause();
                            for(ConstraintViolation<?> cv : cve.getConstraintViolations()) {
                                formErrors.add(cv.getMessage());
                            }
                        }
                    }
                }
            }
        }
        finally {
            em.close();
            fact.close();
        }
        
        if (formSuccess) {
            response.sendRedirect("EditFolder?success=true");
        }
        else {
            RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/views/editFolder.jsp");
            dispatcher.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}